package com.briup.jz.service.impl;


import com.briup.jz.bean.Address;
import com.briup.jz.bean.AddressExample;
import com.briup.jz.bean.extend.AddressExtend;
import com.briup.jz.dao.AddressMapper;
import com.briup.jz.dao.extend.AddressExtendMapper;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Resource
    private AddressMapper addressMapper;

    @Resource
    private AddressExtendMapper addressExtendMapper;

    @Override
    public List<Address> findAll() {
        return addressMapper.selectByExample(new AddressExample());
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        Address address = addressMapper.selectByPrimaryKey(id);
        if(address == null){
            throw new CustomerException("删除失败，要删除的数据不存在");
        }

        addressMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void saveOrUpdate(Address address) throws CustomerException {
        if(address.getId() == null){
            addressMapper.insert(address);
        }else {
           addressMapper.updateByPrimaryKey(address);
        }

    }


    @Override
    public List<AddressExtend> findAllWithUser() {
       return addressExtendMapper.selectAllWithUser();
    }
}

